import {lazyReport} from '../report/report';

// history中,五种路由变化,pushState只能监听到go,forward,back,不能监听pushState和replaceState
//因此要重写pushState和replaceState方法
// history路由监听
export function historyPageTrackerReport() {
    let beforeTime = Date.now(); // 刚开始进入页面的时间
    let beforePage = ''; // 上一个页面

    // 获取在某个页面的停留时间
    function getStayTime() {
        let curTime = Date.now();
        let stayTime = curTime - beforeTime;
        beforeTime = curTime;
        return stayTime;
    }

    // 重写pushState和replaceState方法
    const createHistoryEvent = function (name) {
        // 拿到原来的处理方法
        const origin = window.history[name];
        return function (event) {
            // if (name === 'replaceState') {
            //   const { current } = event;
            //   const pathName = location.pathname;
            //   if (current === pathName) {
            //     let res = origin.apply(this, arguments);
            //     return res;
            //   }
            // }
            let res = origin.apply(this, arguments);
            //这里用到了自定义事件
            let e = new Event(name);
            e.arguments = arguments;
            window.dispatchEvent(e);
            return res;
        };
    };

    window.addEventListener('pushState', function () {
        listener()
    });
    window.addEventListener('replaceState', function () {
        listener()
    });

    window.history.pushState = createHistoryEvent('pushState');
    window.history.replaceState = createHistoryEvent('replaceState');

    function listener() {
        const stayTime = getStayTime(); // 停留时间
        const currentPage = window.location.href; // 页面路径
        lazyReport('visit', {
            stayTime,
            page: beforePage,
        })
        beforePage = currentPage;
    }

    // 页面load监听
    window.addEventListener('load', function () {
        // beforePage = location.href;
        listener()
    });

    // unload监听
    window.addEventListener('unload', function () {
        listener()
    });

    // history.go()、history.back()、history.forward() 监听
    window.addEventListener('popstate', function () {
        listener()
    });
}

// hash,五种路由变化,pushState不能监听pushState
//因此要重写pushState方法
 // hash路由监听
export function hashPageTrackerReport() {
    let beforeTime = Date.now(); // 进入页面的时间
    let beforePage = ''; // 上一个页面

    function getStayTime() {
        let curTime = Date.now();
        let stayTime = curTime - beforeTime;
        beforeTime = curTime;
        return stayTime;
    }

    function listener() {
        const stayTime = getStayTime();
        const currentPage = window.location.href;
        lazyReport('visit', {
            stayTime,
            page: beforePage,
        })
        beforePage = currentPage;
    }
    // hash路由监听
    window.addEventListener('hashchange', function () {
        listener()
    });
    // 页面load监听
    window.addEventListener('load', function () {
        listener()
    });

    const createHashEvent = function (name) {
        const origin = window.history[name];
        return function (event) {
            // if (name === 'replaceState') {
            //   const { current } = event;
            //   const pathName = location.pathname;
            //   if (current === pathName) {
            //     let res = origin.apply(this, arguments);
            //     return res;
            //   }
            // }
            let res = origin.apply(this, arguments);
            let e = new Event(name);
            e.arguments = arguments;
            window.dispatchEvent(e);
            return res;
        };
    };

    window.history.pushState = createHashEvent('pushState');
    window.addEventListener('pushState', function () {
        listener()
    });
}